//
//  PhonePswLoginView.m
//  BaseProject
//
//  Created by 王洋 on 2021/1/20.
//  Copyright © 2021 王洋. All rights reserved.
//

#import "PhonePswLoginView.h"

@implementation PhonePswLoginView

+ (CGFloat)calculateHeight {
    return 168;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.isShowPsw = NO;
    
    UIView *phoneView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 23, 46)];
    phoneView.backgroundColor = [UIColor clearColor];
    self.phoneTF.leftView =phoneView;
    self.phoneTF.leftViewMode = UITextFieldViewModeAlways;
    
    UIView *pswView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 23, 46)];
    pswView.backgroundColor = [UIColor clearColor];
        
    UIView *pswRighrView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 46)];
    pswRighrView.backgroundColor = [UIColor clearColor];
    [pswRighrView addSubview:self.changeBtn];

    self.pswTF.leftView =pswView;
    self.pswTF.leftViewMode = UITextFieldViewModeAlways;
    self.pswTF.rightView =pswRighrView;
    self.pswTF.rightViewMode = UITextFieldViewModeAlways;
    self.pswTF.secureTextEntry = YES;
    
    NSAttributedString *phoneAttrString = [[NSAttributedString alloc] initWithString:@"请输入手机号" attributes:
    @{NSForegroundColorAttributeName:UIColorHex(0xd8d8d8),
                 NSFontAttributeName:self.phoneTF.font
         }];
    self.phoneTF.attributedPlaceholder = phoneAttrString;
    
    NSAttributedString *pswAttrString = [[NSAttributedString alloc] initWithString:@"请输入密码" attributes:
    @{NSForegroundColorAttributeName:UIColorHex(0xd8d8d8),
                 NSFontAttributeName:self.pswTF.font
         }];
    self.pswTF.attributedPlaceholder = pswAttrString;
    
    WEAKSELF
    [[RACSignal merge:@[self.phoneTF.rac_textSignal, RACObserve(self.phoneTF, text)]] subscribeNext:^(NSString* text){

        if (weakSelf.phoneTF.text.length == 11 && [Utilities checkPhoneTel:weakSelf.phoneTF.text]) {
            [weakSelf.btnBGImageView setImage:[UIImage imageNamed:@"rect_orange"]];
            weakSelf.btnBGImageView.alpha = 1;
            [weakSelf.handleBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
            weakSelf.handleBtn.enabled = YES;

        } else {
            [weakSelf.btnBGImageView setImage:[UIImage imageNamed:@""]];
            weakSelf.btnBGImageView.alpha = 0.8;
            [weakSelf.handleBtn setTitleColor:UIColorHex(0x777777) forState:UIControlStateNormal];
            weakSelf.handleBtn.enabled = NO;
        }
    }];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.btnBGImageView pp_clipCorners:UIRectCornerAllCorners radius:self.btnBGImageView.frame.size.height/2];
        
    });
}

- (IBAction)handleAction:(id)sender {
    if (self.PhonePswLoginViewBlock) {
        self.PhonePswLoginViewBlock(PhonePswActionType_login);
    }
}

- (void)changeShowStatus {
    
    self.isShowPsw = !self.isShowPsw;
    if (self.isShowPsw) {
        self.pswTF.secureTextEntry = NO;
        [self.changeBtn setImage:[UIImage imageNamed:@"cansee"] forState:UIControlStateNormal];
    } else {
        self.pswTF.secureTextEntry = YES;
        [self.changeBtn setImage:[UIImage imageNamed:@"cantSee"] forState:UIControlStateNormal];

    }
}

- (UIButton *)changeBtn {
    if (!_changeBtn) {
        _changeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_changeBtn setImage:[UIImage imageNamed:@"cantSee"] forState:UIControlStateNormal];
        _changeBtn.frame = CGRectMake(0, 0, 60, 46);
        [_changeBtn addTarget:self action:@selector(changeShowStatus) forControlEvents:UIControlEventTouchUpInside];
    }
    return _changeBtn;
}




@end
